package com.llh.controller.http;

import com.llh.bean.FinaIndicator;
import com.llh.bean.MiddleData;
import com.llh.bean.StockBasic;
import com.llh.controller.http.common.CommonExcuteTrade;
import com.llh.mapper.FinaIndicatorMapper;
import com.llh.mapper.StockBasicMapper;
import com.llh.util.SqlSessionUtil;
import lombok.Data;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;

@Data
public class FinaIndicatorTrade extends CommonExcuteTrade {
    private MiddleData middleData;
    static Logger logger = LogManager.getLogger(FinaIndicatorTrade.class.getName());

    /**
     * 处理响应的数据的param参数
     *
     * @param items  参数值二元数组:items=[[a1,b1,c1...],[c2,c2,c2...]....] ;
     * @param fields 参数名数组:fields = [a,b,c....]
     * @throws Exception
     */
    @Override
    public void parseParam(String[][] items, String[] fields) throws Exception {
        //获取sqlSession
        SqlSession sqlSesion = null;

        //该接口只能一次调一只股票的,这里我们根据查询条件只会返回一条数据
        List<FinaIndicator> list = (List<FinaIndicator>) copyProperty(items, fields);
            /*if(list.size()!=1){
                throw new Exception("数据有误");
            }*/
        if (list == null || list.size() == 0) {
            logger.error("没有获取到任何数据");
            return;
        }
        FinaIndicator finaIndicator = list.get(0);
        finaIndicator.setOpen_day(middleData.getOpen_day());
        try {
            sqlSesion = SqlSessionUtil.getSqlSesion();
            FinaIndicatorMapper mapper = sqlSesion.getMapper(FinaIndicatorMapper.class);
            logger.info("插入数据");
            mapper.insertSelective(finaIndicator);
            logger.info("插入操作成功");

        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("将数据插入到fina_indicator失败");
        } finally {
            sqlSesion.close();
        }
    }

    /**
     * 封装响应数据
     *
     * @param items
     * @param fields
     * @return
     * @throws Exception
     */
    @Override
    public List copyProperty(String[][] items, String[] fields) throws Exception {
        List<FinaIndicator> finaIndicators = new LinkedList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        if (items == null || items.length == 0) {
            //没有数据则返回空list
            return finaIndicators;
        }
        for (int j = 0; j < items.length; j++) {
            //参数值数组
            String[] item = items[j];
            if (item == null || item.length == 0) {
                //如果有数据组为null,为空数组,跳下一次循环
                continue;
            }
            FinaIndicator finaIndicator = new FinaIndicator();
            Field[] declaredFields = finaIndicator.getClass().getDeclaredFields();

            //有效数据数据格式校验
            if (item.length != fields.length) {
                throw new Exception("响应的数据有误");
            }
            for (int i = 0; i < fields.length; i++) {
                //参数名
                String field = fields[i];
                //参数值
                String value = item[i];
                for (Field declaredField : declaredFields) {
                    declaredField.setAccessible(true);
                    String fieldName = declaredField.getName();
                    if (field.equals(fieldName)) {
                        //如果返回来的值为null,字符串"null",空字符串"",将对应参数设置为null
                        if (value == null || "null".equals(value.toLowerCase()) || "".equals(value)) {
                            declaredField.set(finaIndicator, null);
                        } else {
                            //若果是正常值,则转换为对应的类型
                            if (field.equals("ann_date") || field.equals("end_date")) {
                                //将字符串转换成date.并赋值
                                declaredField.set(finaIndicator, format.parse(value));
                            } else if (field.equals("roe") || field.equals("debt_to_assets")) {
                                //将字符串转换成double
                                declaredField.set(finaIndicator, Double.valueOf(value));
                            } else {
                                //直接保留原字符串格式
                                declaredField.set(finaIndicator, value);
                            }
                        }


                    }
                }
            }
            //添加到list集合
            finaIndicators.add(finaIndicator);

        }
        return finaIndicators;
    }
}
